#include "scheduler/repeat_scheduler.h"
#include "../policy/policy_factory.h"
#include "../policy/behavior_tree/behavior_tree_repeat_policy.h"

namespace scheduler
{
RepeatScheduler::~RepeatScheduler() = default;
RepeatScheduler::RepeatScheduler(const std::string &name)
    : AbstractScheduler(name)
{
    schedule_policy_ = policy::PolicyFactory<policy::BehaviorTreeRepeatPolicy>::createPolicy(name, 1);
}
RepeatScheduler::RepeatScheduler(const std::string &name, int repeat_count)
    : AbstractScheduler(name)
{
    schedule_policy_ = policy::PolicyFactory<policy::BehaviorTreeRepeatPolicy>::createPolicy(name, repeat_count);
}
bool RepeatScheduler::appendTask(const task::AbstractTaskConstPtr &task)
{
    EASE_ERROR("repeat scheduler cannot add task");
    throw std::runtime_error("repeat scheduler cannot add task");
}
bool RepeatScheduler::appendScheduler(const AbstractSchedulerConstPtr &scheduler)
{
    EASE_ERROR("repeat scheduler cannot add scheduler");
    throw std::runtime_error("repeat scheduler cannot add scheduler");
}
void RepeatScheduler::setRepeatCount(int32_t count)
{
    auto policy = dynamic_cast<policy::BehaviorTreeRepeatPolicy *>(schedule_policy_.get());
    if (nullptr == policy)
    {
        throw std::runtime_error("repeat scheduler case fail");
    }
    policy->setRetryCount(count);
}
int32_t RepeatScheduler::getRepeatCount() const
{
    auto policy = dynamic_cast<policy::BehaviorTreeRepeatPolicy *>(schedule_policy_.get());
    if (nullptr == policy)
    {
        throw std::runtime_error("repeat scheduler case fail");
    }
    return policy->getRetryCount();
}
}
